package TUI;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
import java.util.Scanner;

import ModelLayer.*;
import CTRLayer.tController;


public class createOrderMenu
{
    // instance variables - replace the example below with your own
    private tController ctr;

    /**
     * Constructor for objects of class OrderUI
     */
    public createOrderMenu()
    {
        // initialise instance variables
        ctr = new tController();
    }

    public void orderMenu()
    {
        boolean exit = false;
        while (!exit)
        {
            int choice = writeOrderMenu();
            if(choice == 1)
            {
                createOrder();
            }
            else
            {
                    if (choice == 2)
                    { 
                        deleteOrder();
                    }
                    else
                    {
                            if (choice == 3)
                            {
                                listAllOrders();
                            }
                            else
                            {
                                writeEnd();
                                exit = true;
                            }//end else
                    }// end else
            }//end else
        }//end while
    }//end start
    
    private int writeOrderMenu()
    {       
        // creates a keyboard object to read input
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** Order Menu ***");
        System.out.println(" (1) Create Order");
        System.out.println(" (2) Delete Order");
        System.out.println(" (3) List All Orders");
        System.out.println(" (4) Close");
        System.out.print("\n Make your choice: ");
            
        int choice = keyboard.nextInt();
            
        return choice;
    }
    
    private int inputOrderId()
    {   
        Scanner keyboard = new Scanner(System.in);
        int orderId = keyboard.nextInt();
        
        return orderId;
    }
    
    private int inputProductId()
    {   
        Scanner keyboard = new Scanner(System.in);
        int productId = keyboard.nextInt();
        
        return productId;
    }
    
    private int inputCustomerId()
    {   
        Scanner keyboard = new Scanner(System.in);
        int customerId = keyboard.nextInt();
        
        return customerId;
    }
    
    private int inputDeliveryId()
    {   
        Scanner keyboard = new Scanner(System.in);
        int deliveryId = keyboard.nextInt();
        
        return deliveryId;
    }
    
    private String inputString()
    {   
        Scanner keyboard = new Scanner(System.in);
        String stringInput = keyboard.nextLine();
        
        return stringInput;
    }
    
    private int inputInt()
    {   
        Scanner keyboard = new Scanner(System.in);
        int intInput = keyboard.nextInt();
        
        return intInput;
    }
    
    private void createOrder()
    {	
    	Date creationDate = null;
    	Date paymentDate = null;
    	
    	System.out.println("\nSelect a customer ID from the list: ");
        for(Customer c : ctr.listAllCustomers())
        {
        	System.out.println("Customer ID: " + c.getId() + " Name: " + c.getName() + " Surname: " + c.getSurname());
        }
        int customerId = inputInt();
        
        System.out.println("\nSelect a delivery ID from the list: ");
        for(Delivery d : ctr.listAllDelivery())
        {
        	System.out.println("Customer ID: " + d.getDeliveryId() + " Name: " + d.getDeliveryCost() + " Surname: " + d.getDeliveryState());
        }
        int deliveryId = inputInt();
        
        System.out.println("\nInsert a creation date format DD-MM-YYYY: ");
        try {
			creationDate = new SimpleDateFormat("DD-MM-YYYY", Locale.ENGLISH).parse(inputString());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        System.out.println("\nInsert a payment date format DD-MM-YYYY: ");
        try {
			paymentDate = new SimpleDateFormat("DD-MM-YYYY", Locale.ENGLISH).parse(inputString());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        System.out.println("\nInsert total: ");
        int total = inputInt();
        
        System.out.println("\nInsert status: ");
        String status = inputString();
        
        System.out.println("\nInsert discount: ");
        int discount = inputInt();
        
        boolean choseProducts = true;
        ArrayList<SalesLine> salesList = new ArrayList<SalesLine>();
        while(choseProducts)
        {
        	System.out.println("\nSelect a product ID from the list (type -1 to exit): ");
        	for(Product p : ctr.listAllProducts())
        	{
        		System.out.println("\nProduct ID: " + p.getProductId() + " Name: " + p.getName() + " Price: " + p.getSalesPrice());
        	}
        	int productId = inputProductId();
        	if(productId==-1)
        	{
        		choseProducts=false;
        	}
        	else
        	{
	        	System.out.println("\nInsert amount: ");
	        	int quantity = inputInt();
	        	salesList.add(new SalesLine(quantity, ctr.getProductById(productId)));
        	}
        }
        
		ctr.createOrder(customerId, creationDate, paymentDate, total, status, discount, deliveryId, salesList);
    }
    
    private void deleteOrder()
    {
        System.out.println("\nSelect a order ID from the list: ");
        listAllOrders();
        int orderId = inputOrderId();
        ctr.deleteOrder(orderId);
    }
    
    private void listAllOrders()
    {
    	System.out.println("\nAll Orders: ");
    	for(SalesOrder so : ctr.listAllOrders())
    	{
    		System.out.println("\nID: " + so.getOrderId() + " Total: " + so.getAmount());
    	}
    }
    
    private void writeEnd()
    {
        System.out.println(" The System is shuting down ");
        System.exit(0);
    }
}
